
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */
package com.hlkj.pay.app.common.impl;

import com.hlkj.pay.infrastructure.model.admin.AdminUserDO;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.hlkj.framework.common.provider.Aes256EncryptionProvider;
import com.hlkj.pay.service.admin.IAdminUserService;
import com.hlkj.pay.vo.common.req.EmailSendReq;
import com.hlkj.pay.vo.common.req.SmsSendReq;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author monk@techsun.com
 * @date 2023/04/25 14:33
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class MessageConsumerSelector {

    private final Aes256EncryptionProvider aes256EncryptionProvider;
    
    private final IAdminUserService userService;

    /**
     * 测试接收者处理
     * @param receiver
     * @return
     */
    public String testReceiver(String receiver) {
        try {
            return aes256EncryptionProvider.encryptString(receiver);
        } catch (Exception e) {
            log.error("encrypt error:{}", e.getMessage(), e);
        }
        return receiver;
    }
    
    /**
     * 手机号
     * @param smsSendReq
     * @return
     */
    public void phoneConsumerSelect(SmsSendReq smsSendReq) {
        String phone = smsSendReq.getPhone();
        if (StringUtils.isEmpty(phone)) {
            AdminUserDO adminUserDO;
            if(smsSendReq.getUserId()!=null){
                adminUserDO = userService.queryUser(smsSendReq.getUserId());
            }else {
                adminUserDO = userService.queryUserBySn(smsSendReq.getSn());
            }
            phone= adminUserDO.getPhoneEncrypt();
        }
        try {
            smsSendReq.setPhone(aes256EncryptionProvider.encryptString(phone));
        } catch (Exception e) {
            smsSendReq.setPhone(null);
            log.error("encrypt error:{}", e.getMessage(), e);
        }
    }
    /**
     * 手机号
     * @param smsSendReq
     * @return
     */
    public void emailConsumerSelect(EmailSendReq smsSendReq) {
        String email = smsSendReq.getEmail();
        if (StringUtils.isEmpty(email)) {
            AdminUserDO adminUserDO;
            if(smsSendReq.getUserId()!=null){
                adminUserDO = userService.queryUser(smsSendReq.getUserId());
            }else {
                adminUserDO = userService.queryUserBySn(smsSendReq.getSn());
            }
            email= adminUserDO.getEmailEncrypt();
        }
        try {
            smsSendReq.setEmail(aes256EncryptionProvider.encryptString(email));
        } catch (Exception e) {
            smsSendReq.setEmail(null);
            log.error("encrypt error:{}", e.getMessage(), e);
        }
    }
    
}
